﻿#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Weazel.Gezel.Model.Errors;

#endregion

namespace Weazel.Toad.Application.Controls
{ 
  [ToolboxItem(true)]
  public partial class MessageListControl : UserControl
  {
    private delegate void AddMessagesDelegate();
    private AddMessagesDelegate addMessagesDelegate;

    public MessageListControl()
    {
      InitializeComponent();
      addMessagesDelegate = new AddMessagesDelegate(addMessages);

      if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
      {
        listView.Items.Clear();

        Application.Instance.GezelModelAnalysis.ParsingCompleted 
          += new EventHandler<EventArgs>(GezelModelAnalysis_ParsingCompleted);
      }
    }

    private void GezelModelAnalysis_ParsingCompleted(object sender, EventArgs e)
    {
      Invoke(addMessagesDelegate);
    }

    private void addMessages()
    {
      GezelModelAnalysis analysis = Application.Instance.GezelModelAnalysis;

      // clear old items
      listView.Items.Clear();

      // add parse errors
      foreach (ParseError error in analysis.ParseErrors)
      {
        ErrorMessage message = error.ErrorMessage;
        if(message.Position != null)
          addMessage(false, message.ToString(), message.Position.File, message.Position.LineNumber);
        else
          addMessage(false, message.ToString());
      }

      // add verification errors
      foreach (VerificationError error in analysis.VerificationErrors)
      {
        ErrorMessage message = error.ErrorMessage;
        if (message.Position != null)
          addMessage(false, message.ToString(), message.Position.File, message.Position.LineNumber);
        else
          addMessage(false, message.ToString());
      }

			foreach (VerificationWarning warning in analysis.Warnings)
			{
				ErrorMessage message = warning.ErrorMessage;
				if (message.Position != null)
					addMessage(true, message.ToString(), message.Position.File, message.Position.LineNumber);
				else
					addMessage(true, message.ToString());
			}
    }

    private void addMessage(bool isWarning, string description, string file, uint lineNumber)
    {
      System.Windows.Forms.ListViewItem messageItem = new System.Windows.Forms.ListViewItem(new string[] {
            string.Empty,
            description,
            file,
            lineNumber.ToString()}, 
						isWarning ? 1 : 0);

      listView.Items.Add(messageItem);
    }
    
    private void addMessage(bool isWarning, string description)
    {
      System.Windows.Forms.ListViewItem messageItem = new System.Windows.Forms.ListViewItem(new string[] {
            string.Empty,
            description,
            string.Empty,
            string.Empty}, 
						isWarning ? 1 : 0); // 

      listView.Items.Add(messageItem);			
    }
  }
}
